﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Fis.Web.Rules.Book
{
    public class ImportRule : DynamicRule
    {
        public override void ExecuteNonTrans()
        {
            var transType = Paramater["TransType"].ToString();

            switch (transType)
            {
                case "1":
                    ImportAccount();
                    break;
                case "2":
                    ImportAids();
                    break;
                case "3":
                    ImportRate();
                    break;
            }
        }

         void ImportAccount()
        {
            var dbTable = "TACCOUNT";
            var data = Agile.Engine.Utils.ZipUtils.UnZipDataTable(Paramater["data"].ToString());
            //覆盖模式删除所有非系统数据
            if (Paramater["ImportType"].Equals("1"))
            {
                Model.Delete(dbTable, "FFIXEDACCOUNT=0 or FFIXEDACCOUNT is null");
                CommonRule common = new CommonRule();
                common.UpdateDeleteInfo(Model.DataContext, dbTable, string.Empty, BookID, 3);
            }

            var schema = Model.QueryTable(dbTable, "2=1");

            foreach (var d in data.AsEnumerable())
            {
                {
                    d["SINDEXDATE"] = DateTime.Now;
                    d["SBOOKID"] = BookID;
                    schema.Rows.Add(d.ItemArray);
                }
            }

            Model.UpdateTable(schema);

            if (data.Rows.Count > 0)
                Result["data"] = Agile.Engine.Utils.ZipUtils.ZipDataTable(data);
        }

        void ImportAids()
        {
            var dbTable = "TAIDS";
            var data = Agile.Engine.Utils.ZipUtils.UnZipDataTable(Paramater["data"].ToString());

            //覆盖模式删除所有非系统数据
            if (Paramater["ImportType"].Equals("1"))
            {
                Model.Delete(dbTable, "1=1");
                CommonRule common = new CommonRule();
                common.UpdateDeleteInfo(Model.DataContext, dbTable, string.Empty, BookID, 3);
            }

            var schema = Model.QueryTable(dbTable, "2=1");

            foreach (var d in data.AsEnumerable())
            {                
                {
                    d["SID"] = Guid.NewGuid().ToString();
                    d["SINDEXDATE"] = DateTime.Now;
                    d["SBOOKID"] = BookID;
                    schema.Rows.Add(d.ItemArray);
                }
            }

            Model.UpdateTable(schema);
            
            if (data.Rows.Count > 0)
                Result["data"] = Agile.Engine.Utils.ZipUtils.ZipDataTable(data);
        }

        void ImportRate()
        {
            var dbTable = "TRATE";
            var data = Agile.Engine.Utils.ZipUtils.UnZipDataTable(Paramater["data"].ToString());

            //覆盖模式删除所有非系统数据
            if (Paramater["ImportType"].Equals("1"))
            {
                Model.Delete(dbTable, "FISBASE is null or FISBASE != 1");
                CommonRule common = new CommonRule();
                common.UpdateDeleteInfo(Model.DataContext, dbTable, string.Empty, BookID, 3);
            }


            var schema = Model.QueryTable(dbTable, "FISBASE=1");

            List<DataRow> delList = new List<DataRow>();
            foreach (var d in data.AsEnumerable())
            {
                //非系统数据导入
                if (d["FCODE"].Equals(schema.Rows[0]["FCODE"]))
                {
                    delList.Add(d);
                }
                else
                {
                    d["SID"] = Guid.NewGuid().ToString();
                    d["SINDEXDATE"] = DateTime.Now;
                    d["SBOOKID"] = BookID;
                    schema.Rows.Add(d.ItemArray);
                }
            }

            Model.UpdateTable(schema);

            foreach (var d in delList)
            {
                data.Rows.Remove(d);
            }

            if (data.Rows.Count > 0)
                Result["data"] = Agile.Engine.Utils.ZipUtils.ZipDataTable(data);
        }
    }
}
